<?php

namespace ProjetA\GroupeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use ProjetA\GroupeBundle\Entity\Date as Date;
use ProjetA\GroupeBundle\Entity\Page as Page;
use ProjetA\UserBundle\Entity\User as User;

/**
 * ProjetA\GroupeBundle\Entity\Groupe
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="ProjetA\GroupeBundle\Entity\GroupeRepository")
 */
class Groupe {

    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $nom
     *
     * @ORM\Column(name="nom", type="string", length=255)
     */
    private $nom;

    /**
     * @var datetime $datecreation
     *
     * @ORM\Column(name="datecreation", type="datetime")
     */

    private $datecreation;

     /**
     * @var boolean $public
     *
     * @ORM\Column(name="public", type="boolean")
     */
    private $public;
    
    /**
     * @var text $description
     *
     * @ORM\Column(name="description", type="text")
     */
    private $description;

    

    /**
     * Bidirectional Fils
     *
     * @ORM\ManyToMany(targetEntity="ProjetA\UserBundle\Entity\User", mappedBy="demandes")
     */
    private $demandeurs;

    /**
     *
     * @ORM\ManyToMany(targetEntity="ProjetA\UserBundle\Entity\User", mappedBy="inscriptions")
     */
    private $inscrits;

    /**
     * Bidirectional - proprietaire
     *
     * @ORM\ManyToOne(targetEntity="ProjetA\UserBundle\Entity\User", inversedBy="groupes")
     */
    private $moderateur;

    /**
     *
     * @ORM\OneToMany(targetEntity="ProjetA\GroupeBundle\Entity\Post", mappedBy="groupe", orphanRemoval="true")
     */
    private $posts;
 
    
    
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
    
    /**
     * @ORM\ManyToOne(targetEntity="ProjetA\GroupeBundle\Entity\Page")
     */
    private $page;
    
    /**
     * @ORM\OneToMany(targetEntity="ProjetA\GroupeBundle\Entity\Text" , cascade={"remove"},mappedBy="groupe", orphanRemoval="true")
     */
    private $texts;
    
        
    /**
     * @ORM\OneToMany(targetEntity="ProjetA\GroupeBundle\Entity\Date" , cascade={"remove"},mappedBy="groupe", orphanRemoval="true")
     */
    private $dates;
    /**
     * @ORM\OneToMany(targetEntity="ProjetA\GroupeBundle\Entity\Tag", mappedBy="groupe", orphanRemoval="true")
     */
    private $tags;
    
     function __construct() {
        $this->demandeurs = new \Doctrine\Common\Collections\ArrayCollection();
        $this->posts = new \Doctrine\Common\Collections\ArrayCollection();
        $this->texts = new \Doctrine\Common\Collections\ArrayCollection();
        $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
    }
    public function getTags() {
        return $this->tags;
    }

    public function setTags($tags) {
        $this->tags = $tags;
    }

        
    public function getPage() {
        return $this->page;
    }

    public function setPage(Page $page) {
        $this->page = $page;
    }

    public function getTexts() {
        return $this->texts;
    }
    public function getDates() {
        return $this->dates;
    }

    public function setDates($dates) {
        $this->dates = $dates;
    }

    public function getTextByName($name){
        foreach ($this->texts as $text) {
            if($name == $text->getName()){
                return $text->getText();
            }            
        }
        return false;
    }
    public function getDateByName($name){
        foreach ($this->dates as $date) {
            if($name == $date->getName()){
                return $date->getDate();
            }            
        }
        return false;
    }

    public function setTexts($texts) {
        $this->texts = $texts;
    }
    public function addText(Text $text){
        $this->texts[] = $text;
    }
    public function addDate(Date $date){
        $this->dates[] = $date;
    }

        
    
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=


   public function getPosts() {
        return array_reverse($this->posts->toArray());
    }

    public function setPosts($posts) {
        $this->posts = $posts;
    }

    public function addPost(Post $post) {
        $this->posts->add($post);
    }

    public function suppPost(Post $post) {
       $this->posts->removeElement($post);
    }

    public function getDemandeurs() {
        return $this->demandeurs;
    }

    public function getInscrits() {
        return $this->inscrits;
    }

    public function getModerateur() {
        return $this->moderateur;
    }

    public function addInscrit(User $user) {
        $this->inscrits->add($user);
    }
    public function suppInscrit(User $user){
        $this->inscrits->removeElement($user);
    }

    public function addDemande(User $user) {
        $this->demandeurs->add($user);
    }
     
    public function suppDemande(User $user){
        $this->demandeurs->removeElement($user);
    }

    public function setModerateur(User $moderateur) {
        $this->moderateur = $moderateur;
    }
    public function isPublic() {
        return $this->public;
    }

    public function setPublic($public) {
        $this->public = $public;
    }
  

    /*
     * Est-ce qu'un utilisateur est inscrit dans le groupe.
     */

    public function estInscrit($user){
       return $this->inscrits->contains($user);
    }
       /*
     * Est-ce qu'un utilisateur a fait une demande d'inscription dans le groupe.
     */

    public function estDemandeur($user){
       return $this->demandeurs->contains($user);
    }
    
    
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set nom
     *
     * @param string $nom
     */
    public function setNom($nom) {
        $this->nom = $nom;
    }

    /**
     * Get nom
     *
     * @return string 
     */
    public function getNom() {
        return $this->nom;
    }

    /**
     * Set datecreation
     *
     * @param datetime $dateCreation
     */
    public function setDatecreation($dateCreation) {
        $this->datecreation = $dateCreation;
    }

    /**
     * Get datecreation
     *
     * @return datetime 
     */
    public function getDatecreation() {
        return $this->datecreation;
    }

    /**
     * Set description
     *
     * @param text $description
     */
    public function setDescription($description) {
        $this->description = $description;
    }

    /**
     * Get description
     *
     * @return text 
     */
    public function getDescription() {
        return $this->description;
    }


}
